<template>
	<view class="wrap">
		<view class="addrlist">
			<van-radio-group v-model="checked">
				<view class="item" v-for="v in addrs">
					<view class="top">
						<view class="one">
							<text class="name">{{v.name}}</text>
							<text class="tel">{{v.tel}}</text>
							<van-tag color="#Fe9b23" plain text-color="grey" size="medium"
								v-if="v.isDefault">默认</van-tag>
						</view>
						<p class="info">{{v.province}}{{v.city}},{{v.county}}{{v.addressDetail}}</p>
					</view>
					<view class="bottom">
						<van-radio :name="v.addressId" checked-color="#Fe9b23"> 选择此地址</van-radio>
						<view class="right">
							<view class="" @click="edit(v.addressId)">
								<van-icon name="edit" /><text>编辑</text>
							</view>
							<view class="" @click="del(v.addressId)">
								<van-icon name="delete-o" /><text>删除</text>
							</view>
						</view>
					</view>
				</view>
			</van-radio-group>
		</view>
		<view class="btn">
			<van-button round color="#Fe9b23" style="width: 92vw;height: 36px;font-size: 36rpx;"
				@click="addaddr()">添加新收货地址</van-button>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		onBeforeUnmount,
		watch
	} from 'vue'
	import $http from '../../request/index.js'
	import {
		onShow,
		onLoad
	} from '@dcloudio/uni-app'
	import {
		useStore
	} from 'vuex'
	import {
		showSuccessToast,
		showFailToast
	} from 'vant'
	const store = useStore()
	let addrs = ref('')
	let checked = ref('');

	watch(checked, val => {
		console.log(val)
		store.commit('updateAddressId', val)
	})

	function addaddr() { //新增地址
		uni.navigateTo({
			url: '/pages/addaddr/addaddr'
		})
	}

	function edit(id) { //点击编辑地址
		console.log(id)
		uni.navigateTo({
			url: '/pages/addaddr/addaddr?id=' + id
		})
	}

	function del(id) { //删除地址
		console.log('删除地址')
		$http.get(`/address/delete/${id}`).then(res => {
			console.log(res)
			let {
				code,
				msg
			} = res
			if (code) { //弹窗提示删除成功与否
				showFailToast(msg)
			} else {
				showSuccessToast(msg)
			}
		})
	}
	onShow(() => {
		let address = uni.getStorageSync('addrslist')
		// 如果里面有东西就直接用
		if (address) {
			addrs.value = address
		} else {
			// 没有就请求地址列表数据
			$http.get('/address/list').then(res => {
				console.log(res)
				addrs.value = res.data
				uni.setStorageSync('addrlist', res.data)
			})
		}
	})
</script>

<style scoped>
	.right {
		width: 250rpx;
		justify-content: space-between;
		align-items: center;
		display: flex;
	}

	.one {
		display: flex;
		justify-content: flex-start;
		align-items: center;
	}

	.van-tag {
		margin-left: 80rpx;
	}

	.info {
		font-size: 24rpx;
		margin: 20rpx 0;
	}

	.name {
		font-size: 36rpx;
		font-weight: bold;
	}

	.tel {
		font-weight: bold;
		margin-left: 40rpx;
	}

	.addrlist {
		width: 100vw;
		padding: 20rpx;
		box-sizing: border-box;
	}

	.item {
		width: 100%;
		background-color: #ffffff;
		border-radius: 20rpx;
		padding: 20rpx;
		box-sizing: border-box;
		margin: 20rpx 0;
	}

	.top {
		border-bottom: 1px solid #ccc;
	}

	.bottom {
		width: 100%;
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.btn {
		width: 100vw;
		position: absolute;
		left: 0;
		bottom: 0;
		border-top: 1px solid gray;
		height: 50px;
		padding: 14rpx;
		box-sizing: border-box;
		text-align: center;
		background-color: #ffffff;
	}

	.wrap {
		width: 100vw;
		height: calc(100vh - 94px);
		overflow-y: scroll;
		padding-bottom: 50px;
		background-color: #f7f7f7;
	}
</style>